﻿using System;
using System.Collections.Generic;
using System.Linq;
using Shop.Base.Entity;
using Shop.Base.Entity.Delete;
using Shop.Base.Interface;
using Shop.Server;

namespace Shop.Base.Server
{
    public class SystemUserRole:BaseServer<SystemUserRolePo>, ISystemUserRole
    {
        public bool MultSave(List<SystemUserRolePo> list,Guid userId)
        {
            try
            {
                var savedlist = GetList(userId);
                db.Ado.BeginTran();
                if (savedlist.Any())
                {
                    //删除已有的角色
                    var del = savedlist.Select(c => new DeleteSystemUserRole {Id = c.Id}).ToList();
                    db.Deleteable(del).ExecuteCommand();
                }
                if (list.Any())
                {
                    db.Insertable(list).ExecuteCommand();
                }
                db.Ado.CommitTran();
                return true;
            }
            catch (Exception e)
            {
              db.Ado.RollbackTran();
                throw;
            }
        }

        public List<SystemUserRolePo> GetList(Guid userId)
        {
            return db.Queryable<SystemUserRolePo>().Where(x => x.UserId == userId).ToList();
        }
    }
}